<!-- 护理等级列表页 -->
<template>
  <div class="serveGrade">
    <div class="header">
      <h2>护理等级管理</h2>
    </div>
    <div class="content">
      <SearchForm :searchData="searchData" @handleSearch="handleSearch" @handleReset="handleReset">
      </SearchForm>

      <TableList :listData="listData" :total="total" :pagination="pagination" @get-current="getCurrent"
        @handleBulid="handleBulid" @handleEdit="handleEdit" @handleClickDelete="handleClickDelete"
        @handleForbidden="handleForbidden">
      </TableList>

      <DialogForm :visible="formVisible" :title="title" :data="data" :planData="planData" @handleClose="handleClose"
        @handleEditForm="handleEditForm" @handleAdd="handleAdd">
      </DialogForm>

      <!-- 删除弹层 -->
      <Delete :visible="dialogDeleteVisible" :delete-text="operateText" @handle-delete="handleDelete"
        @handle-close="handleDeleteClose"></Delete>
      <!-- end -->
      <!-- 禁用弹层 -->
      <Forbidden :visible="visible" :text="operateText" @handle-submit="handleSubmit"
        @handle-close="handleForbiddenClose"></Forbidden>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { getLevelList, levelDelete, levelStatus, getLevelDetails, levelUpdate, levelAdd, getnursAllPlanList } from '@/api/serve'
import { MessagePlugin } from 'tdesign-vue-next'
import TableList from './components/TableList.vue';
import SearchForm from './components/SearchForm.vue';
import DialogForm from './components/DialogFrom.vue';
// 删除弹层
import Delete from '@/components/OperateDialog/index.vue'
// 禁用弹窗
import Forbidden from '@/components/Forbidden/index.vue'

const planData = ref([]);



const listData = ref([]) // 列表数据
const total = ref(0) // 总条数
const pagination = ref({
  pageNum: 1,
  pageSize: 10,
}) // 分页
const dataLoading = false // 加载状态
const searchData = ref({
  name: '',
  status: ''
})

const formVisible = ref(false)
const title = ref('新增');
// 新增护理等级
const handleBulid = () => {
  formVisible.value = true;
  title.value = '新增';
}
//取消按钮
const handleClose = () => {
  formVisible.value = false;
}

const data = ref({})

// 编辑护理等级
const handleEdit = async (row) => {
  const result = await getLevelDetails(row.id);
  data.value = result.data;
  formVisible.value = true;
  title.value = '编辑';

}


const handleEditForm = (formData) => {
  levelUpdate(formData);
  formVisible.value = false;
  query();
  MessagePlugin.success('编辑成功');
}

const handleAdd = (formData) => {
  levelAdd(formData);
  formVisible.value = false;
  query();
  MessagePlugin.success('编辑成功');
}


const dialogDeleteVisible = ref(false);
// 删除护理等级
let r = '';
let operateText = '护理等级';
const handleClickDelete = (row) => {
  dialogDeleteVisible.value = true;
  r = row.id;
}
const handleDeleteClose = () => {
  dialogDeleteVisible.value = false;
}
const handleDelete = async () => {
  await levelDelete(r);
  dialogDeleteVisible.value = false;
  query();
}


const visible = ref(false);
const val = ref({});
// 禁用/启用护理等级
const handleForbidden = (row) => {
  visible.value = true;
  val.value = row;
}
const handleForbiddenClose = () => {
  visible.value = false;
}
const handleSubmit = async () => {
  if (val.value.status === 1) {
    await levelStatus({ id: val.value.id, status: 0 });
    MessagePlugin.success('禁用成功');
    visible.value = false;
    query();
  } else {
    await levelStatus({ id: val.value.id, status: 1 });
    visible.value = false;
    MessagePlugin.success('启用成功');
    query();
  }
}

// 获取当前页的数据
const query = async () => {
  const levelList = await getLevelList({ pageNum: pagination.value.pageNum, pageSize: pagination.value.pageSize, name: searchData.value.name, status: searchData.value.status });
  listData.value = levelList.data.records;
  total.value = levelList.data.total;
}

const querypPlanList = async () => {
  const planList = await getnursAllPlanList();
  planData.value = planList.data;
};
onMounted(async () => {
  // 在组件挂载时执行的操作，例如获取列表数据
  await query();
  await querypPlanList();
});
//换页
const getCurrent = (pageInfo) => {
  pagination.value.pageNum = pageInfo.current;
  pagination.value.pageSize = pageInfo.pageSize;
  query();

}

//搜索
const handleSearch = () => {
  query();
}

const handleReset = () => {
  searchData.value.name = '';
  searchData.value.status = '';
  query();
}

</script>